﻿namespace CarAppDto.Dtos
{
    /// <summary>查询条件 And Or 查询 不支持嵌套</summary>
    public class FilterDto : IEqualityComparer<FilterDto>
    {
        /// <summary>
        /// 字段名称
        /// </summary>
        public string Field { get; set; }

        /// <summary>
        /// And 连接 默认
        /// </summary>
        public bool IsAnd { get; set; } = true;

        /// <summary>
        /// 操作符 like 、 = 、！= 、> 、< 、>= 、<= 、in 、 between 
        /// </summary>
        public string op { get; set; } = "=";

        /// <summary>
        /// 值
        /// </summary>
        public object Value { get; set; }

        public static List<FilterDto> Filters(params FilterDto[] filter)
        {
            var filers = new List<FilterDto>(filter);
            return filers.Distinct(new FilterDto()).ToList();
        }

        public bool Equals(FilterDto me, FilterDto other)
        {
            var result = me.Field == other.Field
                && me.Value.ToString() == other.Value.ToString()
                && me.op == other.op
                && me.IsAnd == other.IsAnd;
            return result;
        }

        public int GetHashCode(FilterDto me)
        {
            return me.ToString().GetHashCode();
        }
    }

    /// <summary>
    /// 排序
    /// </summary>
    public class SortDto
    {
        /// <summary>
        /// 字段
        /// </summary>
        public string Field { get; set; }

        /// <summary>
        /// 排序类型
        /// </summary>
        public string Sort { get; set; } = "ASC";
    }
}
